//
//  XueInfoController.m
//  GuShiNews
//
//  Created by 丁一 on 2019/8/23.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "XueInfoController.h"
#import "LearnHomeHeaderView.h"
#import "NewsModel.h"
#import "NewsInfoShowCell.h"
#import "XueXiModel.h"
#import "JoneCunChu.h"
#import "MementoCenter.h"
#import "NSObject+MementoCenter.h"

#import "HiJonesClass.h"

@interface XueInfoController ()<UINavigationControllerDelegate>{
    BOOL cun_ok;
}

@property (nonatomic, strong) NSMutableArray     *Like_list;
@property (nonatomic, strong) NSMutableArray     *Look_list;
@property (nonatomic, strong) UIView              *Button_view;

@end

@implementation XueInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.TableView];
    self.navigationItem.title=_Model.x_tittle;
    self.TableView.frame=CGRectMake(0, 0, RECRWITCH, RECERHEIGHT-(NAV_Height-20));

    [self.TableView registerClass:[LearnHomeHeaderView class] forHeaderFooterViewReuseIdentifier:@"LearnHomeHeaderView"];
    [self cunJubao];
    
    NSArray *image_list = [[NSArray alloc] initWithObjects:@"news_back",@"",@"",@"fenxiang", nil];
    
    __block CGFloat l_w = RECRWITCH/image_list.count;
    
    _Button_view = [[UIView alloc] initWithFrame:CGRectMake(0, RECERHEIGHT-(NAV_Height-20), RECRWITCH, NAV_Height-25)];
    _Button_view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_Button_view];
    
    [image_list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx==0) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(l_w * idx, 5, l_w, self->_Button_view.frame.size.height-10)];
            button.tag = idx;
            [button setImage:[UIImage imageNamed:obj] forState:UIControlStateNormal];
            button.imageView.contentMode = UIViewContentModeScaleAspectFit;
            [button addTarget:self action:@selector(touch_buttom:) forControlEvents:UIControlEventTouchUpInside];
            [self->_Button_view addSubview:button];
        }else if(idx==1){
            UILabel *title = [[ UILabel alloc ] initWithFrame:CGRectMake(RECRWITCH/2-80, 5, 160, self->_Button_view.frame.size.height-10)];
            title.textAlignment=NSTextAlignmentCenter;
            title.font = [UIFont systemFontOfSize:17];
            if (self->cun_ok) {
                 title.textColor = [UIColor grayColor];
                 title.text=@"您已学完此节";
            }else{
                title.textColor = [UIColor greenColor];
                title.text=@"正在学习..";
            }
            [self->_Button_view addSubview:title];
        }
    }];
    
    
    CALayer *topline = [CALayer new];
    topline.frame=CGRectMake(0, 0, RECRWITCH, 0.5);
    topline.backgroundColor = ColorMoren.CGColor;
    [_Button_view.layer addSublayer:topline];
}

- (void)touch_buttom:(UIButton *)sender{
    if (sender.tag ==0) {
        [self goback];
    }else{
        NSString *textToShare = _Model.x_tittle;
        UIImage *imageToShare = [UIImage imageNamed:@"logo.png"];
        NSURL *urlToShare = [NSURL URLWithString:@"mixixi.cc"];
        NSArray *items = @[textToShare,imageToShare];
        [HiJonesClass mq_share:items target:self success:^(BOOL fff, NSString *ggg) {
            
        }];
    }
}

- (void)cunJubao{
    __weak __typeof(&*self)weakSelf = self;
    NSMutableArray *jubaolist = [NSMutableArray new];
    @try {
        JoneCunChu *shuju=[[JoneCunChu alloc] init];
        
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"XuexiArray"]) {
            [shuju recoverFormStateWithKey:@"XuexiArray"];
            jubaolist = shuju.JoneArray;
        }
        cun_ok = NO;
        [jubaolist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XueXiModel *linmodel = (XueXiModel *)obj;
            if ([linmodel.x_id isEqualToString:weakSelf.Model.x_id]) {
                self->cun_ok = YES;
                *stop = YES;
            }
        }];
        if (!cun_ok) {
            [jubaolist addObject:_Model];
        }
        shuju.JoneArray=jubaolist;
        [shuju saveStateWithKey:@"XuexiArray"];
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LearnHomeHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LearnHomeHeaderView"];
    view.Model = _Model;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (_Model.x_logo.length<10) {
        return _Model.title_height+30 + 25;
    }
    return _Model.title_height + 30 + 20 + (RECRWITCH-50)/3*2 + 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _Model.info_height+20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *modelin = [NewsModel new];
    modelin.news_info = _Model.x_info;
    
    NewsInfoShowCell *cell = [NewsInfoShowCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model            = modelin;
    return cell;
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
    if(viewController == self){
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }else{
        [self.navigationController setNavigationBarHidden:NO animated:animated];
        if(self.navigationController.delegate == self){
            self.navigationController.delegate = nil;
        }
    }
}

@end
